package com.easy.easylibdemo.ui.mulitedittext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.easy.easylibdemo.R;

import java.util.ArrayList;

public class MuliteditActivity extends AppCompatActivity {

    EditText et1, et2, et3, et4;
    ArrayList<EditText> etList;
    int empty = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mulitedit);
        et1 = (EditText) findViewById(R.id.et_1);
        et2 = (EditText) findViewById(R.id.et_2);
        et3 = (EditText) findViewById(R.id.et_3);
        et4 = (EditText) findViewById(R.id.et_4);
        etList = new ArrayList<>();
        etList.add(et1);
        etList.add(et2);
        etList.add(et3);
        etList.add(et4);
        aaa();
    }

    private void aaa() {

        for (int i = 0; i < etList.size(); i++) {

            final int finalI = i;

            etList.get(i).setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    /**
                     * 当前输入框都已填写文字时，用户点了前面的et获取焦点时，把焦点设置在最后一个et中，并且光标置于最后
                     */
                    if (hasFocus && empty == etList.size() && finalI != empty - 1) {
                        etList.get(empty - 1).requestFocus();
                        etList.get(empty - 1).setSelection(etList.get(empty - 1).getText().toString().length());
                    }
                    /**
                     * a.获取焦点时，如果所有的et并没有填满，那么重置焦点到标记为空的输入框
                     * b.把输入框内容清除，这一步是为了删除回退上一个et用的
                     */
                    if (hasFocus && empty != etList.size()) {
                        etList.get(empty).requestFocus();
                        etList.get(empty).setText("");
                    }
                }
            });
            etList.get(i).setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    /**
                     * 删除时调整empty
                     */
                    if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_UP) {
                        empty--;
                        if (empty < 0) {
                            //全删了
                            empty = 0;
                        } else {
                            etList.get(empty).requestFocus();
                        }
                        return false;
                    }
                    /**
                     * 屏蔽回车键，避免软键盘回车键进入下一个et
                     */
                    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                        return true;
                    }
                    return false;
                }
            });

            etList.get(i).addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    /**
                     * 输入非空文字后，空输入框标记位++
                     * 如果et都填完了，不做焦点相关操作
                     * 否则重置输入框焦点
                     */
                    if (!TextUtils.isEmpty(s.toString())) {
                        empty++;
                        if (empty == etList.size()) {
                            //et都填完了
                        } else {
                            etList.get(empty).requestFocus();
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    }
}
